import webpack, {ProgressPlugin, BannerPlugin} from "webpack";
import path from 'path';
import ForkTSCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
import MiniExtractWebpackPlugin from "mini-css-extract-plugin";
import CopyWebpackPlugin from "copy-webpack-plugin";
import HtmlWebpackPlugin from "html-webpack-plugin";

const MiniExtractLoader = {
  loader: MiniExtractWebpackPlugin.loader,
  options: {
    publicPath: "/",
    hmr: false,
  },
};

module.exports = () => {
  const config: webpack.Configuration = {
    entry: path.resolve(__dirname, "src/index.ts"),
    mode: "development",
    infrastructureLogging: {
      level: "verbose",
    },
    // stats: {
    //   logging: "verbose",
    // },
    target: "web",
    output: {
      libraryTarget: "umd",
    },
    devtool: false, // "source-map",
    // watch: true,
    resolve: {
      extensions: [".tsx", ".ts", ".js", ".json"],
      fallback: {
        assert: require.resolve("assert/"),
        util: require.resolve("util/"),
        url: require.resolve("url/"),
        path: require.resolve("path-browserify"),
        buffer: require.resolve("buffer"),
        stream: require.resolve("stream-browserify"),
        process: require.resolve("process/browser.js"),
        crypto: require.resolve("crypto-browserify"),
        zlib: require.resolve("browserify-zlib"),
        https: require.resolve("https-browserify"),
        http: require.resolve("stream-http"),
        vm: require.resolve("vm-browserify"),
        os: require.resolve("os-browserify/browser"),
        constants: require.resolve("constants-browserify"),
      },
    },
    module: {
      rules: [
        {
          test: /\.(png|jpe?g|gif)$/i,
          type: "asset",
        },
        {
          test: /\.less$/,
          use: [
            "style-loader",
            {
              loader: require.resolve("css-loader"),
              options: {},
            },
            {
              loader: require.resolve("postcss-loader"),
              options: {
                postcssOptions: {
                  plugins: [
                    require("autoprefixer")(),
                  ],
                },
              },
            },
            {
              loader: require.resolve("less-loader"),
              options: {},
            },
          ],
        },
        {
          test: /\.css$/,
          use: [
            "style-loader",
            {
              loader: require.resolve("css-loader"),
              options: {},
            },
            {
              loader: require.resolve("postcss-loader"),
              options: {
                postcssOptions: {
                  plugins: [
                    require("autoprefixer")(),
                  ],
                },
              },
            },
          ],
        },
        {
          test: /\.md$/,
          resourceQuery: /rsmi/,
          use: [
            "vue-loader",
            {
              loader: require.resolve(
                path.resolve(__dirname, "build/vsmi-md-loader/cjs.js")
              ),
              options: {
                type: "code",
              },
            },
          ],
        },
        {
          test: /\.md$/,
          resourceQuery: (v) => {
            if (!v) {
              return true;
            }
            return !/rsmi/.test(v);
          },
          use: [
            {
              loader: require.resolve(
                path.resolve(__dirname, "build/vsmi-md-loader/cjs.js")
              ),
              options: {
                type: "source",
              },
            },
          ],
        },
        {
          test: /\.tcss$/,
          use: [
            {
              loader: require.resolve(
                path.resolve(__dirname, "build/wx-loader/cjs.js")
              ),
              options: {
                type: "source",
              },
            },
            {
              loader: require.resolve(
                path.resolve(__dirname, "build/smi-loader/cjs.js")
              ),
              options: {
                type: "MTX",
              },
            },
          ],
        },
        {
          test: /\.tsx?$/,
          loader: "swc-loader",
        },
      ],
    },
    plugins: [
      new CopyWebpackPlugin({
        patterns: [
          {
            from: path.resolve(__dirname, "public"),
            to: path.resolve(__dirname, "dist"),
            noErrorOnMissing: true,
          },
        ],
        options: {},
      }),
      new HtmlWebpackPlugin(),
      new ProgressPlugin(),
      new BannerPlugin({
        banner: "SMI-Builder 2022",
      }),
      new ForkTSCheckerWebpackPlugin()
    ]
  };
  return config;
};
